<template>
  <div class="divBox">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <div class="container" v-hasPermi="['platform:agreement:page:list']">
          <el-form size="small" label-width="100px" :inline="true">
            <el-form-item label="协议名称：" label-width="100px" style="display: inline-block">
              <el-input v-model.trim="tableFrom.name" @keyup.enter.native="getList(1), getHeadNum()"
                placeholder="请输入关键字搜索" class="selWidth">
                <el-button slot="append" icon="el-icon-search" class="el-button-solt" @click="getList(1), getHeadNum()" />
              </el-input>
            </el-form-item>
            <el-tabs v-if="headeNum.length > 0" v-model="tableFrom.enabled" @tab-click="getList(1), getHeadNum()">
              <el-tab-pane v-for="(item, index) in headeNum" :key="index" :name="item.type.toString()"
                :label="item.title + '(' + item.count + ')'" />
            </el-tabs>
          </el-form>
        </div>
        <el-button size="small" type="primary" v-hasPermi="['platform:agreement:update:agreement']"
          @click="onAdd">添加协议</el-button>
      </div>
      <el-table v-loading="listLoading" :data="tableData.data" style="width: 100%" size="small" highlight-current-row
        class="switchTable">
        <el-table-column prop="id" label="ID" min-width="60" />
        <el-table-column prop="name" label="协议名称" min-width="150" />
        <el-table-column prop="createTime" label="创建时间" min-width="150" />
        <el-table-column prop="status" label="开启/隐藏" min-width="90" fixed="right">
          <template slot-scope="scope">
            {{ scope.row.enabled ? '开启' : '关闭' }}
            <!-- <el-switch v-if="checkPermi(['platform:agreement:open', 'platform:agreement:close'])"
              v-model="scope.row.enabled" active-text="开启" inactive-text="关闭" disabled
              @click.native="onchangeIsClose(scope.row)" />
            <div v-else>{{ scope.row.enabled ? '开启' : '关闭' }}</div> -->
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="110" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="onEdit(scope.row.id, 1)" class="mr10"
              v-hasPermi="['platform:agreement:detail']">编辑</el-button>
            <!-- <el-dropdown trigger="click" v-if="checkPermi([
              'platform:agreement:update:agreements',
            ])
              ">
              <span class="el-dropdown-link"> 更多<i class="el-icon-arrow-down el-icon--right" /> </span>
              <el-dropdown-menu slot="dropdown" class="icon-arrow-down">
                <el-dropdown-item @click.native="onEdit(scope.row.id)"
                  v-if="checkPermi(['platform:agreement:update:agreements'])">编辑</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown> -->
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination :page-sizes="[20, 40, 60, 80]" :page-size="tableFrom.limit" :current-page="tableFrom.page"
          layout="total, sizes, prev, pager, next, jumper" :total="tableData.total" @size-change="handleSizeChange"
          @current-change="pageChange" />
      </div>
    </el-card>

    <el-dialog title="协议" width="1000px" :visible.sync="addAgreementDialogVisible" :before-close="closeModel"
      :closeOnClickModal="false" append-to-body>
      <creat-agreement ref="creatAgreement" :merId="merId" :key="indexKey" :is-disabled="isDisabled"
        @closeModel="closeModel" @saveFinish="saveFinish()"></creat-agreement>
    </el-dialog>

    <el-dialog title="协议" width="1000px" :visible.sync="addAgreementDialogVisible" :before-close="closeModel"
      :closeOnClickModal="false" append-to-body>
      <creat-agreement ref="editAgreement" :merId="merId" :agreementId="agreementId" :key="indexKey"
        @closeModel="closeModel" @saveFinish="saveFinish()"></creat-agreement>
    </el-dialog>
  </div>
</template>
<script>
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
import * as agreement from '@/api/agreement';
import creatAgreement from './creatAgreement';
import { mapGetters } from 'vuex';
import { checkPermi } from '@/utils/permission'; // 权限判断函数

export default {
  name: 'AgreementList',
  components: { creatAgreement },
  props: {
    merId: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      addAgreementDialogVisible: false,
      fromList: this.$constants.fromList,
      isChecked: false,
      listLoading: false,
      headeNum: [
        {
          count: '',
          type: '1',
          title: '正常开启的协议',
        },
        {
          count: '',
          type: '0',
          title: '已关闭协议',
        },
      ],
      tableData: {
        data: [],
        total: 0,
      },
      tableFrom: {
        page: 1,
        limit: 20,
        enabled: true,
      },
      autoUpdate: true,
      timeVal: [],
      agreementId: 0,
      keyNum: 0,
      isDisabled: false,
      indexKey: 0,
    };
  },
  computed: {
  },
  mounted() {
    if (checkPermi(['platform:agreement:list'])) this.getHeadNum();
    if (checkPermi(['platform:agreement:page:list'])) this.getList('');
  },
  methods: {
    checkPermi,
    /**
     *  选择时间
     */
    selectChange(tab) {
      this.tableFrom.dateLimit = tab;
      this.timeVal = [];
      this.tableFrom.page = 1;
      this.tableData.data = [];
      this.getList('');
      this.getHeadNum();
    },
    /**
     *  具体日期
     */
    onchangeTime(e) {
      this.timeVal = e;
      this.tableFrom.dateLimit = this.timeVal ? this.timeVal.join(',') : '';
      this.tableFrom.page = 1;
      this.getList('');
      this.getHeadNum();
    },
    /**
     *  获取开启协议数
     */
    getHeadNum() {
      const data = { ...this.tableFrom };
      data.merchantId = this.merId;
      delete data.page;
      delete data.limit;
      agreement
        .agreementHeaderNumApi(data)
        .then((res) => {
          this.headeNum[0]['count'] = res.openNum;
          this.headeNum[1]['count'] = res.closeNum;
        })
        .catch((res) => { });
    },
    saveFinish() {
      this.addAgreementDialogVisible = false
      this.getHeadNum()
      this.getList('')
    },
    /**
     *  列表
     */
    getList(num) {
      this.listLoading = true;
      this.tableFrom.page = num ? num : this.tableFrom.page;
      agreement
        .agreementListApi({
          name: this.tableFrom.name,
          nameLike: true,
          pageNo: this.tableFrom.page,
          pageSize: this.tableFrom.limit,
          enabled: this.tableFrom.enabled,
          merchantId: this.merId,
          fetchMerchant: true
        })
        .then((res) => {
          this.tableData.data = res.list;
          this.tableData.total = res.total;
          this.listLoading = false;
        })
        .catch((res) => {
          this.listLoading = false;
          this.$message.error(res.message);
        });
    },
    pageChange(page) {
      this.tableFrom.page = page;
      this.getList('');
      this.getHeadNum();
    },
    handleSizeChange(val) {
      this.tableFrom.limit = val;
      this.getList(1);
      this.getHeadNum();
    },
    getChange() {
      this.getHeadNum();
      this.getList(1);
      this.addAgreementDialogVisible = false;
    },
    closeModel() {
      this.addAgreementDialogVisible = false;
    },
    /**
     *  添加
     */
    onAdd() {
      this.addAgreementDialogVisible = true;
      this.isDisabled = false;
      this.indexKey = Math.random();
      this.agreementId = 0;
    },
    /**
     *  编辑
     */
    onEdit(id, n) {
      this.addAgreementDialogVisible = true;
      this.agreementId = id;
      this.indexKey = Math.random();
      // n ? (this.isDisabled = true) : (this.isDisabled = false);
    },
  },
};
</script>

<style scoped lang="scss">
.demo-table-expand label {
  width: 90px;
}

.el-dropdown-link {
  cursor: pointer;
  color: #1890ff;
  font-size: 12px;
}

.el-icon-arrow-down {
  font-size: 12px;
}

.icon-arrow-down {
  ::v-deep .el-dropdown-menu__item {
    font-size: 12px;
  }
}
</style>
